package com.example.demo.controller;

import com.example.demo.dao.GraduateUserMapper;
import com.example.demo.entity.GraduateUser;
import com.example.demo.service.GraduateUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
public class GraduateUserController {

    @Resource
    private GraduateUserMapper graduateUserMapper;

    @Resource
    private GraduateUserService graduateUserService;

    @GetMapping("/")
    public String login(){
        //System.out.println(graduateUserMapper.findAll());
        return "login";
    }

    @GetMapping("/index")
    public String index(){
        return "index";
    }

    @PostMapping("/login")
    public String login(@RequestParam("username") String username, @RequestParam("password")
            String password, HttpSession session){
        GraduateUser graduateUser = graduateUserService.findUser(username, password);
        if (graduateUser != null){
            session.setAttribute("graduateUser", graduateUser.getGraduateUsername());
            session.setAttribute("graduateUserId", graduateUser.getGraduateUserId());
            //session过期时间设置为7200秒 即两小时
            //session.setMaxInactiveInterval(60 * 60 * 2);
            return "redirect:/index";
        }else{
            session.setAttribute("errorMsg","登陆失败");
            return "login";
        }
    }




}
